package com.abso.sunlight.explorer.dialogs;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.abso.sunlight.explorer.CongressExplorerPlugin;
import com.abso.sunlight.explorer.SunlightImages;

/**
 * The dialog used to save a search by typing its unique name.
 */
public class SaveLegislatorSearchDialog extends TitleAreaDialog {

    /** The initial name of the search. */
    private String initialSearchName;

    /** The final name of the search. */
    private String searchName;

    /** The text field where the name is typed. */
    private Text nameText;

    /**
     * Constructs a new dialog.
     * 
     * @param parentShell
     *            the parent shell.
     * @param initialSearchName
     *            the initial name of the search.
     */
    public SaveLegislatorSearchDialog(Shell parentShell, String initialSearchName) {
        super(parentShell);
        this.initialSearchName = initialSearchName;
    }

    @Override
    public void create() {
        super.create();
        getShell().setText("Save");
        setTitle("Legislator Search");
        setTitleImage(SunlightImages.SAVE_WIZ.get());
        setMessage("Save the search with a specific name.");
        setErrorMessage(null);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    @Override
    protected Control createDialogArea(Composite parent) {
        Control dialogArea = super.createDialogArea(parent);

        /* adds the main composite */
        Composite mainArea = new Composite((Composite) dialogArea, SWT.NONE);
        mainArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        mainArea.setLayout(new GridLayout(2, false));

        /* adds the name field. */
        Label nameLabel = new Label(mainArea, SWT.NONE);
        nameLabel.setText("Name:");
        nameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
        nameText = new Text(mainArea, SWT.BORDER);
        nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        nameText.setText(StringUtils.defaultString(initialSearchName));
        nameText.selectAll();
        nameText.addModifyListener(new ModifyListener() {
            public void modifyText(ModifyEvent e) {
                validateName();
                refreshOKButton();
            }
        });
        return dialogArea;
    }

    @Override
    protected void createButtonsForButtonBar(Composite parent) {
        super.createButtonsForButtonBar(parent);
        refreshOKButton();
    }

    /**
     * Validates the current name.
     */
    private void validateName() {
        setErrorMessage(computeErrorMessage());
    }

    /**
     * Computes the current error message.
     * 
     * @return the current error message, or <code>null</code> if the current name is valid.
     */
    private String computeErrorMessage() {
        String name = StringUtils.trim(nameText.getText());
        if (name.length() == 0) {
            return "Name must be specified.";
        } else if (CongressExplorerPlugin.getSearchManager().getSearch(name) != null) {
            return "A saved search with that name already exists.";
        } else {
            return null;
        }
    }

    /**
     * Refreshes the Ok button based on the validity of the current name.
     */
    private void refreshOKButton() {
        getButton(IDialogConstants.OK_ID).setEnabled(computeErrorMessage() == null);

    }

    @Override
    protected void buttonPressed(final int buttonId) {
        if (buttonId == IDialogConstants.OK_ID) {
            searchName = nameText.getText();
        }
        super.buttonPressed(buttonId);
    }

    /**
     * Returns the current validated name for the search.
     * 
     * @return   the current name for the search.
     */
    public String getSearchName() {
        return searchName;
    }

}
